package com.bkgrands.fpmu.Adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bkgrands.fpmu.R;
import com.bkgrands.fpmu.activities.MstagesActivity;
import com.bkgrands.fpmu.classes.Payload;
import com.bkgrands.fpmu.classes.StaticFarm;

import java.util.List;

/**
 * Created by jeff on 7/11/19.
 */


public class SiteAdapter extends RecyclerView.Adapter<SiteAdapter.ViewHolder> {
    private List<Payload> payloads;
    Context context;
    public SiteAdapter(Context context, List<Payload> payloads) {
        this.payloads = payloads;
        this.context = context;
    }



    @Override
    public SiteAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.allpendingworks, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final SiteAdapter.ViewHolder viewHolder, final int i) {

        viewHolder.tv_facility.setText(payloads.get(i).getFacility());
        viewHolder.tv_company.setText(payloads.get(i).getContractor_name());


        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                StaticFarm.currentSite = SiteAdapter.this.payloads.get(i);

                Intent intent = new Intent(v.getContext(), MstagesActivity.class);
                intent.putExtra( "facility", String.valueOf(payloads.get(i).getFacility()));
                context.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        return payloads.size();
    }

    public void updateData(List<Payload> payload) {
        this.payloads  = payload;
        this.notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_facility,tv_company;
        public ViewHolder(View view) {
            super(view);

            tv_facility = (TextView)view.findViewById(R.id.site);
            tv_company = (TextView)view.findViewById(R.id.company);

        }
    }

}

